/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package crawler.impl;

import com.google.common.base.Joiner;
import crawler.common.PostData;
import crawler.itf.PostImpl;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author LT
 */
public class BetaBongDaComVnParser extends PostImpl {

    static final Logger logger = Logger.getLogger(BetaBongDaComVnParser.class);

    @Override
    public List<PostData> getListTitle(Document doc) {

        List<PostData> lstPost = new LinkedList<>();
        Elements eTitle1 = doc.select(".post-listing li");
        String url1 = eTitle1.select("a[rel=bookmark]").first().attr("href");
            PostData postData1 = new PostData();
            postData1.setUrl(url1);
            String thumbUrl1 = eTitle1.select("img").attr("data-src");
            postData1.setThumb(StaticCrawlerImpl.crawlImage(this, doc,thumbUrl1));
            lstPost.add(postData1);

        Elements eTitle2 = doc.select("article.item-list");
        for (Element e : eTitle2) {
            String url2 = e.select("h2 a[rel=bookmark]").attr("href");
            PostData postData = new PostData();
            postData.setUrl(url2);

            String thumbUrl2 = e.select(".post-thumbnail img").attr("data-src");
            postData.setThumb(StaticCrawlerImpl.crawlImage(this, doc,thumbUrl2));
            lstPost.add(postData);
        }
        return lstPost;
    }

    @Override
    public PostData getDetail(Document doc) {
        String url = doc.location();
        String title = getTitle(doc);
        String cat = getCat(doc);
        String thumbFb = getFbThumb(doc);
        String d = getDesc(doc);
        String ctn = getContent(doc);
        PostData data = null;
        List<String> listImages = new LinkedList<>();

        Elements images = doc.select(".entry img");
        for (Element image : images) {
            String org = image.parent().attr("href");
            org = StaticCrawlerImpl.crawlImage(this, doc,org);
            listImages.add(org);
        }

        List<String> tags = getTags(doc);
        String strTags = Joiner.on(", ").join(tags);
        String jr = getJournalist(doc);
        ctn = removeDescFromContent(ctn, doc);
        ctn = removeJournalistFromContent(ctn, doc);
        ctn = removeIframeFromContent(ctn, doc);
        ctn = removeRelativeNewsFromContent(ctn, doc);
        //check if content is empty
        if (ctn.length() < 100) {
   //         logger.error("Content empty: " + url);
            return null;
        }

        long published = getPublished(doc);

        long crtd = System.currentTimeMillis() / 1000L;
        String site = "beta.bongda.com.vn";
        String fbUrl = getFbUrl(doc);
        String fbTitle = getFbTitle(doc);
        String fbDesc = getFbDesc(doc);
        String fbSite = getFbSite(doc);
        data = new PostData(site,null,cat, "0", title, null, thumbFb, url, d, ctn, listImages, strTags, jr, published, crtd, fbUrl, fbTitle, fbDesc, fbSite);
        return data;
    }

    @Override
    public String getTitle(Document doc) {
        Elements title = doc.select("h1.name.post-title.entry-title");
        String t = title != null ? title.text() : null;
        return t;
    }

    @Override
    public String getCat(Document doc) {
        return "";
   }

    @Override
    public String getDesc(Document doc) {
        Element intro = doc.select(".entry p:eq(0)").first();
        return intro != null ? intro.text().replace("(BongDa.com.vn) – ", "") : null;
    }

    @Override
    public long getPublished(Document doc) {
        long published = 0;
        try {
            //<meta property="article:published_time" content="2014-12-14T20:05:02+00:00" />
            
            Element thumb = doc.head().select("meta[property=article:published_time]").first();
            if(null == thumb) return 0;
            String th = thumb.attr("content");
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
            formatter.parse(th);
            if (th != null && !th.trim().isEmpty()) {
                published = formatter.parse(th).getTime() /1000L;
                published = published + 3600 * 5;
            }
            return published;
        } catch (ParseException ex) {
            java.util.logging.Logger.getLogger(BetaBongDaComVnParser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return published;
    }

    @Override
    public String getContent(Document doc) {
        Elements content = doc.select(".entry");
        Elements images = doc.select(".entry img");
        for (Element image : images) {
            String dataSrc = image.attr("data-src");
            image.attr("src", dataSrc);
        }
        String ctn = content.html();
        return ctn;
    }

    @Override
    public List<String> getTags(Document doc) {
        Elements arrTags = doc.select("p.post-tag a[rel=tag]");
        List<String> lstTags = new LinkedList<>();
        for (Element itemTag : arrTags) {
            String htmlCode = itemTag.text();
            if (htmlCode != null && !htmlCode.trim().isEmpty()) {
                lstTags.add(htmlCode.trim());
            }
        }
        return lstTags;
    }

    @Override
    public String getJournalist(Document doc) {
        String a = "";
        Elements authors = doc.select(".post-source");
        if (authors.size() > 0) {
            Element author = authors.last();
            a = author.text().trim();

        }
        return a;
    }

    public String removeJournalistFromContent(String ctn, Document doc) {
        Elements authors = doc.select(".post-source");
        String htmlAuthor = authors.outerHtml();
        ctn = ctn.replace(htmlAuthor, "");
        return ctn;
    }
    public String removeRelativeNewsFromContent(String ctn, Document doc) {
        Elements widget = doc.select(".entry .widget-top");
        String htmlWidget = widget.outerHtml();
        ctn = ctn.replace(htmlWidget, "");

        Elements rel = doc.select(".entry ul.widget-container");
        String htmlRel = rel.outerHtml();
        ctn = ctn.replace(htmlRel, "");
        
        widget = doc.select(".entry div[align=justify] .widget-top");
        htmlWidget = widget.outerHtml();
        ctn = ctn.replace(htmlWidget, "");

        rel = doc.select(".entry div[align=justify] ul.widget-container");
        htmlRel = rel.outerHtml();
        ctn = ctn.replace(htmlRel, "");
        
       return ctn;
    }
    public String removeIframeFromContent(String ctn, Document doc) {
        Elements iframes = doc.select("iframe");
        for (Element iframe : iframes) {
            String htmlIframe = iframe.outerHtml();
            ctn = ctn.replace(htmlIframe, "");
        }
        return ctn;
    }
    public String removeDescFromContent(String ctn, Document doc) {
        Element intro = doc.select(".entry p:eq(0)").first();
        String htmlIntro = intro.outerHtml();
        ctn = ctn.replace(htmlIntro, "");
        return ctn;
    }
}
